Fix proxy hang when target is unreachable during client-streaming RPCs#618
Merged
Conversation
617f88f to
8277b25
Compare
When NewStream to the target fails (e.g. unreachable host), the TargetStream context was not cancelled. Any in-flight Send() calls blocked forever on the full reqChan, because the cancellation context they select on was never signalled. Add s.cancelFunc() on NewStream failure, matching the existing DialContext failure path. This unblocks Send() and lets the error propagate back to the client. Made-with: Cursor
8277b25 to
dec8f4b
Compare
When grpcStream.SendMsg returns io.EOF (target died), cancel the stream context immediately so TargetStream.Send fails on the next call instead of silently enqueueing data into a dead channel. Also switch Send() to select on s.ctx (the stream's own context) rather than getStream().Context() so it sees the cancellation after setStream() has replaced the placeholder stream. Clear the progress bar on error paths in file cp to avoid showing a confusing 100% after an error message. Made-with: Cursor
c033bce to
a1225b7
Compare
Belt-and-suspenders: s.ctx catches our explicit cancelFunc calls, getStream().Context() catches gRPC-level transport cancellations. Made-with: Cursor
a1225b7 to
2e7a702
Compare
Made-with: Cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed (user-visible)
sanssh file cpthrough the proxy to a target that doesn't exist (or is unreachable) now fails with a clear error instead of hanging indefinitely.Why it was broken
TargetStream.Send()— the proxy method that enqueues data for forwarding to the target — only checked the gRPC stream's context for cancellation. Two problems:Before connection: the gRPC stream context belonged to a placeholder (
unconnectedClientStream) and was never cancelled whenNewStreamfailed.Send()blocked forever on the fullreqChan.After connection: when the target died mid-stream,
grpcStream.SendMsgreturnedio.EOFbut the send loop exited without cancelling anything.Send()kept accepting data into the deadreqChanbecause the buffer had room (consumer gone) and the context wasn't cancelled yet.What was done
proxy/server/target.go:Send()now selects on boths.ctx(our own context, cancelled bycancelFunc) andgetStream().Context()(gRPC transport-level cancellation). Both are needed because aftersetStream()they are different objects.Run()callss.cancelFunc()whenNewStreamfails (was missing).Run()callss.cancelFunc()whenSendMsgreturnsio.EOF(target died mid-stream), soSend()fails immediately on the next call instead of silently enqueueing data.services/localfile/client/client.go:proxy/server/target_test.go:TestSendUnblocksWhenTargetUnreachable— verifiesSendunblocks when the target is unreachable.TestSendFailsAfterTargetDiesMidStream— verifiesSendreturns an error after the target dies mid-stream.